package com.ruoyi.electronicFence.controller;


import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.electronicFence.domain.TEfFenceArea;
import com.ruoyi.electronicFence.service.ITEfFenceAreaService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 围栏区域信息Controller
 *
 * @author ruoyi
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/electronicFence/area")
public class TEfFenceAreaController extends BaseController
{
    @Autowired
    private ITEfFenceAreaService tEfFenceAreaService;

    /**
     * 查询围栏区域信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEfFenceArea tEfFenceArea)
    {
        startPage();
        List<TEfFenceArea> list = tEfFenceAreaService.selectTEfFenceAreaList(tEfFenceArea);
        return getDataTable(list);
    }

    /**
     * 查询围栏区域信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:list')")
    @GetMapping("/details/list")
    public TableDataInfo details(TEfFenceArea tEfFenceArea)
    {
        startPage();
        List<TEfFenceArea> list = tEfFenceAreaService.selectTEfFenceAreaList(tEfFenceArea);
        Long aLong = tEfFenceAreaService.selectTEfFenceAreaListTotal(tEfFenceArea);

        List<Long> ids = list.stream()
                .map(TEfFenceArea::getId)
                .collect(Collectors.toList());
        tEfFenceArea.setIds(ids);
        List<TEfFenceArea> list2 = tEfFenceAreaService.selectTEfFenceAreaDetailsList2(tEfFenceArea);

        TableDataInfo dataTable = getDataTable(list2);
        dataTable.setTotal(aLong);
        return dataTable;
    }

    /**
     * 查询围栏区域信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:list')")
    @GetMapping("/details/list2")
    public TableDataInfo details2(TEfFenceArea tEfFenceArea)
    {
        startPage();
        List<TEfFenceArea> list = tEfFenceAreaService.selectTEfFenceAreaList(tEfFenceArea);
        Long aLong = tEfFenceAreaService.selectTEfFenceAreaListTotal(tEfFenceArea);

        List<Long> ids = list.stream()
                .map(TEfFenceArea::getId)
                .collect(Collectors.toList());
        tEfFenceArea.setIds(ids);
        List<TEfFenceArea> list2 = tEfFenceAreaService.selectTEfFenceAreaDetailsList(tEfFenceArea);

        TableDataInfo dataTable = getDataTable(list2);
        dataTable.setTotal(aLong);
        return dataTable;
    }

    /**
     * 查询场景-设备/场所关联 列表
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:list')")
    @GetMapping("/area-and-equipment/list")
    public TableDataInfo areaAndEquipment(TEfFenceArea tEfFenceArea)
    {
        startPage();
        List<TEfFenceArea> list = tEfFenceAreaService.selectTEfFenceAreaList(tEfFenceArea);
        Long aLong = tEfFenceAreaService.selectTEfFenceAreaListTotal(tEfFenceArea);
        List<Long> ids = list.stream()
                .map(TEfFenceArea::getId)
                .collect(Collectors.toList());
        tEfFenceArea.setIds(ids);

        List<TEfFenceArea> list2 = tEfFenceAreaService.selectAreaAndEquipmentList(tEfFenceArea);

        TableDataInfo dataTable = getDataTable(list2);
        dataTable.setTotal(aLong);
        return dataTable;
    }


    /**
     * 导出围栏区域信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:export')")
    @Log(title = "围栏区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEfFenceArea tEfFenceArea)
    {
        List<TEfFenceArea> list = tEfFenceAreaService.selectTEfFenceAreaList(tEfFenceArea);
        ExcelUtil<TEfFenceArea> util = new ExcelUtil<TEfFenceArea>(TEfFenceArea.class);
        util.exportExcel(response, list, "围栏区域信息数据");
    }

    /**
     * 导出围栏区域信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:export')")
    @Log(title = "围栏区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/details/export")
    public void detailsExport(HttpServletResponse response, TEfFenceArea tEfFenceArea)
    {
        startPage();
        List<TEfFenceArea> list = tEfFenceAreaService.selectTEfFenceAreaList(tEfFenceArea);
        List<Long> ids = list.stream()
                .map(TEfFenceArea::getId)
                .collect(Collectors.toList());
        tEfFenceArea.setIds(ids);
        List<TEfFenceArea> list2 = tEfFenceAreaService.selectTEfFenceAreaDetailsList(tEfFenceArea);

        ExcelUtil<TEfFenceArea> util = new ExcelUtil<TEfFenceArea>(TEfFenceArea.class);
        util.exportExcel(response, list2, "围栏区域信息数据");
    }

    /**
     * 获取围栏区域信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tEfFenceAreaService.selectTEfFenceAreaById(id));
    }

    /**
     * 新增围栏区域信息
     */
    @PreAuthorize("@ss.hasPermi('rsikManage:area:add')")
    @Log(title = "围栏区域信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEfFenceArea tEfFenceArea)
    {
        return toAjax(tEfFenceAreaService.insertTEfFenceArea(tEfFenceArea));
    }


    /**
     * 新增围栏区域详情信息
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:add')")
    @Log(title = "围栏区域信息", businessType = BusinessType.INSERT)
    @PostMapping("/details/insert")
    public AjaxResult detailsInsert(@RequestBody TEfFenceArea tEfFenceArea)
    {
        return toAjax(tEfFenceAreaService.detailsInsertTEfFenceArea(tEfFenceArea));
    }
    /**
     * 修改围栏区域信息
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:edit')")
    @Log(title = "围栏区域信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TEfFenceArea tEfFenceArea)
    {
        return toAjax(tEfFenceAreaService.updateTEfFenceArea(tEfFenceArea));
    }

    /**
     * 修改围栏区域信息
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:edit')")
    @Log(title = "围栏区域信息", businessType = BusinessType.UPDATE)
    @PostMapping("/details/update")
    public AjaxResult detailsUpdate(@RequestBody TEfFenceArea tEfFenceArea)
    {
        return toAjax(tEfFenceAreaService.detailsUpdateTEfFenceArea(tEfFenceArea));
    }

    /**
     * 删除围栏区域信息
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:remove')")
    @Log(title = "围栏区域信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tEfFenceAreaService.deleteTEfFenceAreaByIds(ids));
    }

    /**
     * 删除围栏区域详情信息
     */
//    @PreAuthorize("@ss.hasPermi('rsikManage:area:remove')")
    @Log(title = "围栏区域信息", businessType = BusinessType.DELETE)
    @PostMapping("/details/delete")
    public AjaxResult remove(@RequestBody TEfFenceArea tEfFenceArea)
    {
        return toAjax(tEfFenceAreaService.detailsDeleteTEfFenceAreaByIds(tEfFenceArea));
    }
}
